/**
 * 相似性打分类
 */
package com.enjoy.helper;

import org.apache.lucene.index.FieldInvertState;
import org.apache.lucene.search.similarities.DefaultSimilarity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PageSimilarity extends DefaultSimilarity {

	private static Logger log = LoggerFactory.getLogger(PageSimilarity.class);

	public float lengthNorm(FieldInvertState state) {
		//偏移量相同的词，只计算一次
		int numTerms = state.getLength() - state.getNumOverlap();

		// all字段是二次查询，能命中的则按热度排序
		if (numTerms > 10 || state.getName().equals("all")) {
			numTerms = 10;
		}

		// return state.getBoost() * (float) (1.0f / Math.sqrt(numTerms + 3));
		return state.getBoost() * (1.0f / (float) Math.pow(1.2d, numTerms));
	}

	@Override
	public float tf(float freq) {
		return 1.0f;
	}

	@Override
	public float idf(long docFreq, long numDocs) {
		return 1.0f;
		// return (float)(Math.log(numDocs/(double)(docFreq+1)) + 1.0);
	}
}
